﻿using System;
using System.Windows.Forms;

namespace Expense_Tracker_System
{
    public partial class xExpenseForm : Form
    {
        #region "Declarations"
        aHelperModule helpModule;
        #endregion

        #region "Constructors"
        public xExpenseForm()
        {
            InitializeComponent();
        }
        #endregion

        #region "Properties"
        private TreeView _myTreeView;
        public TreeView myTreeView
        {
            get { return _myTreeView; }
            set { _myTreeView = value; }
        }

        private ImageList _myImageList;
        public ImageList myImageList
        {
            get { return _myImageList; }
            set { _myImageList = value; }
        }
        #endregion

        #region "Commands"
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string _expenseType;
            DateTime _expenseDate;
            string _expenseReceipt;
            decimal _expenseValue;

            _expenseType = txtExpenseType.Text;
            _expenseDate = dtmDate.Value;
            _expenseReceipt = txtReceipt.Text;
            _expenseValue = Convert.ToDecimal(txtValue.Text);

            helpModule = new aHelperModule();
            helpModule.ExpenseType = _expenseType;
            helpModule.ExpenseDate = _expenseDate;
            helpModule.ExpenseReceipt = _expenseReceipt;
            helpModule.ExpenseValue = _expenseValue;

            try
            {

                if ((helpModule.SaveExpense() == 1) & CheckValues(_expenseType) == true & CheckValues(_expenseReceipt) == true & _expenseValue > 0)
                {
                    helpModule.DisplayTreeValues(_myTreeView, _myImageList);
                    MessageBox.Show("Expense saved.", "Expense Tracker", MessageBoxButtons.OK);
                }
                else
                {
                    MessageBox.Show("Error saving expense.", "Expense Tracker", MessageBoxButtons.OK);
                }

            }
            catch (Exception ex)
            {
                if (ex.Message != null)
                    MessageBox.Show("Error saving expense.", "Expense Tracker", MessageBoxButtons.OK);
            }
            finally
            {
                helpModule = null;
                this.Close();
            }
        }

        private void xExpenseForm_Load(object sender, EventArgs e)
        {
            helpModule = new aHelperModule();
            helpModule.LoadComboBoxes(txtExpenseType);
            dtmDate.Value = DateTime.Now;
            txtReceipt.Text = "";
            txtValue.Text = "0";
        }

        private bool CheckValues(string paramThrown)
        {
            bool boolValue = true;

            if (paramThrown == "")
                boolValue = false;

            return boolValue;
        }
        #endregion
    }
}
